package xyz.penghaonan.p2plib.job.operations;

import xyz.penghaonan.p2plib.P2PManager;
import xyz.penghaonan.p2plib.job.base.BaseOperation;
import xyz.penghaonan.p2plib.helper.service.ServiceHelper;

public class StartServiceOperation extends BaseOperation {
    public StartServiceOperation() {
    }

    @Override
    public void doOperation() {
        notifyProgress(0.2f, "开启守护服务");
//        if (P2PManager.getServiceHelper().isServiceEnable()) {
//            setResult(Result.result(true, "守护服务已开启"));
//        } else {
            ServiceHelper.P2PServiceStateListener listener = new ServiceHelper.P2PServiceStateListener() {
                @Override
                public void onStateChanged(boolean enable) {
                    operationContinue();
                }
            };
            P2PManager.getServiceHelper().addStateListener(listener);
            P2PManager.getServiceHelper().start();
            operationWait(3000);
            P2PManager.getServiceHelper().removeStateListener(listener);
            notifyProgress(1f, "守护服务开启成功");
            setResult(Result.result(P2PManager.getServiceHelper().isServiceEnable(), P2PManager.getServiceHelper().isServiceEnable() ? "Start service success" : "Start service failed"));
//        }
    }

    @Override
    public String getName() {
        return "开启守护服务";
    }
}
